/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2023-01-10 09:12:23
 * @LastEditors: Clark
 * @LastEditTime: 2023-01-12 17:50:27
 * @Description: file content
 */

#include "apue.h"
#include <setjmp.h>

static void sig_alrm(int);
static jmp_buf env_alrm;

int main()
{
    int n = -1;
    char line[MAXLINE] = {0};

    if (signal(SIGALRM, sig_alrm) == SIG_ERR)
    {
        err_sys("signal(SIGALRM) error");
    }

    if (setjmp(env_alrm) != 0)
    {
        err_quit("read timeout");
    }

    alarm(10);
    if ((n = read(STDIN_FILENO, line, MAXLINE)) < 0)
    {
        err_sys("read error");
    }
    alarm(0);

    write(STDOUT_FILENO, line, n);
    exit(0);
}

static void sig_alrm(int signo)
{
    printf("longjmp env_alrm\n");
    longjmp(env_alrm, 1);
}
